#include "DaoArticle.h"
#include "SqlDef.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <exception/NpException.h>

using namespace articleInfo;
using namespace chapterInfo;
using namespace npexception;

void DaoArticle::addArticle(const articleInfo::ArticleInfo &article)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_ARTICLE_INSERT, article.articleid.c_str(), article.siteid.c_str(),
          article.postdate.c_str(), article.lastupdate.c_str(), article.articlename.c_str(),
          article.keywords.c_str(), article.initial, article.authorid.c_str(), article.author.c_str(),
          article.posterid.c_str(), article.poster.c_str(), article.agentid.c_str(),
          article.agent.c_str(), article.sortid.c_str(), article.ctypeid.c_str(), article.intro.c_str(),
          article.notice.c_str(), article.setting.c_str(), article.lastvolumeid.c_str(),
          article.lastvolume.c_str(), article.lastchapterid.c_str(), article.lastchapter.c_str(),
          article.chapters.c_str(), article.size.c_str(), article.lastvisit.c_str(), article.dayvisit.c_str(),
          article.weekvisit.c_str(), article.monthvisit.c_str(), article.allvisit.c_str(), article.lastvote.c_str(),
          article.dayvote.c_str(), article.weekvote.c_str(), article.monthvote.c_str(),
          article.allvote.c_str(), article.vipvotetime.c_str(), article.vipvotenow.c_str(),
          article.vipvotepreview.c_str(), article.goodnum.c_str(), article.badnum.c_str(),
          article.toptime.c_str(), article.saleprice.c_str(), article.salenum.c_str(),
          article.totalcost.c_str(), article.articletype, article.permission, article.firstflag, article.fullflag, article.imgflag,
          article.power, article.display,article.downloadlink.c_str()
          );
  doDml(sql);
}

void DaoArticle::updateArticle(const articleInfo::ArticleInfo &article)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_ARTICLE_UPDATE, article.lastvolumeid.c_str(),
          article.lastvolume.c_str(), article.lastchapterid.c_str(), article.lastchapter.c_str(),
          article.chapters.c_str(), article.size.c_str(), article.lastupdate.c_str(),article.articleid.c_str());
  doDml(sql);
}

void DaoArticle::fillArticle(const MYSQL_ROW& row, articleInfo::ArticleInfo &article)
{
  article.articleid.assign(row[articleid]);
  article.siteid.assign(row[siteid]);
  article.postdate.assign(row[postdate]);
  article.lastupdate.assign(row[lastupdate]);
  article.articlename.assign(row[articlename]);
  article.keywords.assign(row[keywords]);
  article.initial = (strlen(row[initial]) == 0) ? '0' : row[initial][0];
  article.authorid.assign(row[authorid]);
  article.author.assign(row[author]);
  article.posterid.assign(row[posterid]);
  article.poster.assign(row[poster]);
  article.agentid.assign(row[agentid]);
  article.agent.assign(row[agent]);
  article.sortid.assign(row[sortid]);
  article.ctypeid.assign(row[ctypeid]);
  article.intro.assign(row[intro]);
  article.notice.assign(row[notice]);
  article.setting.assign(row[setting]);
  article.lastvolumeid.assign(row[lastvolumeid]);
  article.lastvolume.assign(row[lastvolume]);
  article.lastchapterid.assign(row[lastchapterid]);
  article.lastchapter.assign(row[lastchapter]);
  article.chapters.assign(row[chapters]);
  article.size.assign(row[size]);
  article.lastvisit.assign(row[lastvisit]);
  article.dayvisit.assign(row[dayvisit]);
  article.weekvisit.assign(row[weekvisit]);
  article.monthvisit.assign(row[monthvisit]);
  article.allvisit.assign(row[allvisit]);
  article.lastvote.assign(row[lastvote]);
  article.dayvote.assign(row[dayvote]);
  article.weekvote.assign(row[weekvote]);
  article.monthvote.assign(row[monthvote]);
  article.allvote.assign(row[allvote]);
  article.vipvotetime.assign(row[vipvotetime]);
  article.vipvotenow.assign(row[vipvotenow]);
  article.vipvotepreview.assign(row[vipvotepreview]);
  article.goodnum.assign(row[goodnum]);
  article.badnum.assign(row[badnum]);
  article.toptime.assign(row[toptime]);
  article.saleprice.assign(row[saleprice]);
  article.salenum.assign(row[salenum]);
  article.totalcost.assign(row[totalcost]);
  article.articletype = (strlen(row[articletype]) > 0) ? row[articletype][0] : ' ';
  article.permission = (strlen(row[permission]) > 0) ? row[permission][0] : ' ';
  article.firstflag = (strlen(row[firstflag]) > 0) ? row[firstflag][0] : ' ';
  article.fullflag = (strlen(row[fullflag]) > 0) ? row[fullflag][0] : ' ';
  article.imgflag = (strlen(row[imgflag]) > 0) ? row[imgflag][0] : ' ';
  article.power = (strlen(row[power]) > 0) ? row[power][0] : ' ';
  article.display = (strlen(row[display]) > 0) ? row[display][0] : ' ';
}

int DaoArticle::traverseResult(const MYSQL_RES *res, articleInfo::ArticleInfo *articleList)
{
  static MYSQL_ROW row;
  int count= 0;
  while ((row = mysql_fetch_row(const_cast<MYSQL_RES*>(res))) != NULL)
  {
    fillArticle(row, articleList[count]);
    ++count;
  }
  return count;
}

bool DaoArticle::getArticle(const std::string &articleName, articleInfo::ArticleInfo &article)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_ARTICLE_SELECT_BYNAME, articleName.c_str());
  MYSQL_RES* res = doQuery(sql);
  bool toReturn = (traverseResult(res, &article) == 0) ? false : true;
  mysql_free_result(res);
  return toReturn;
}

int DaoArticle::getArticleList(const std::string &lastupdate, articleInfo::ArticleInfo *articleList)
{
  static char sql[SQL_LENGTH];
  sprintf(sql, SQL_ARTICLE_SELECT_LIST, lastupdate.c_str());
  TRAVERSE_AND_RETURN_ROWS(articleList);
}

int DaoArticle::getArticleListByTime(const int lastupdate, articleInfo::ArticleInfo* articleList)
{
  //todo
  return 0;
}

int DaoArticle::getArticleListCount()
{
  MYSQL_RES* res = doQuery(SQL_ARTICLE_SELECT_LIST_COUNT);
  static MYSQL_ROW row;
  row = mysql_fetch_row(res);
  int toReturn = (row == NULL) ? -1 : atoi(row[0]);
  mysql_free_result(res);
  return toReturn;
}

void DaoArticle::getArticleMaxID(std::string &buffer)
{
  buffer = "";
  MYSQL_RES* res = doQuery(SQL_ARTICLE_MAXID);
  static MYSQL_ROW row;
  static char tmp[12];
  while((row = mysql_fetch_row(res)) != NULL)
  {
    if(row[0] != NULL && strlen(row[0]) > 0)
    {
      sprintf(tmp,"%d",atoi(row[0]) + 1);
      buffer.assign(tmp);
    }
  }
  if (buffer.empty() == true) { buffer = "1"; }
  mysql_free_result(res);
}

unsigned int DaoArticle::getArticleMaxID()
{
  unsigned int maxID =1;
  MYSQL_RES* res = doQuery(SQL_ARTICLE_MAXID);
  static MYSQL_ROW row;
  while((row = mysql_fetch_row(res)) != NULL)
  {
    if(row[0] != NULL && strlen(row[0]) > 0)
    {
      maxID = atoi(row[0]) + 1;
    }
  }
  mysql_free_result(res);
  return maxID;
}
